package com.koron.css2.serviceManage.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.BusinessAreaMapper;
import com.koron.css2.serviceManage.queryBean.BusinessAreaQueryBean;
import com.koron.css2.serviceManage.vo.BusinessAreaVo;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 查询营业所下的营业区域
 */
public class BusinessAreaList implements ServerInterface {
    private static Logger logger = LogManager.getLogger(BusinessAreaList.class);

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        @SuppressWarnings("rawtypes")
        MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
        try {
            BusinessAreaMapper mapper = factory.getMapper(BusinessAreaMapper.class);
            BusinessAreaQueryBean bean = JsonUtils.objectToPojo(req.getData(), BusinessAreaQueryBean.class);
            PageHelper.startPage(bean.getPage(), bean.getPageCount());
            bean.setGroupCode(userInfo.getCurWaterCode());
            List<BusinessAreaVo> list = mapper.selectBusinessAreaList(bean);
            info.setData(new PageInfo<>(list));
        } catch (Exception e) {
            info.setCode(Constant.MESSAGE_DBFAIL);
            info.setDescription("数据库异常");
            logger.error("数据库异常", e);
            factory.close(false);
        }
        return info;
    }
}
